package controller;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import pojo.Address;
import pojo.Customer;
import service.CustomerManager;
import service.FileUploadService;
import dto.ResponseDto;

@Controller
public class CustomerController {
	@Resource(name="customerManager")
	private CustomerManager customerManager;
	
    @Resource(name = "configProperties")
    private Properties properties;
    
    @Resource(name="fileUploadService")
    private FileUploadService uploadService;

	@RequestMapping(value = "loginView")
	public String loginView(Model model, HttpServletRequest request){
		model.addAttribute("callback", request.getParameter("callback"));
		return "login";
	}
	
	@RequestMapping(value="/login")
	public String login(String name, String password, Model model, HttpSession session, HttpServletRequest request){
		Customer customer = customerManager.checkCustomer(name, password);
		if (customer == null){
			model.addAttribute("errorInfo", "your username is used by others");
			return "login";
		}
		else{
			session.setAttribute("customer", customer);
			String callback = request.getParameter("callback");
			if(!request.getParameter("callback").equals("")){
				return "redirect:" + callback;
			}else {
				return "redirect:/merchantList.jsp";
			}
		}
	}
	
	@RequestMapping(value="/regist", method=RequestMethod.POST)
	public String regist(Customer c, MultipartFile avatarPic, Model model){
		//上传图片
		System.out.println(avatarPic.getOriginalFilename());
		String savePath = properties.getProperty("image.savePath");
        String[] names = avatarPic.getOriginalFilename().split("\\.");
        String suffix = names[names.length-1];
        String avatarKey = savePath+"avatar/"+UUID.randomUUID().toString()+"."+suffix;
        ResponseDto avatarResponse;
        try {
			avatarResponse = uploadService.upload(properties.getProperty("image.server"),avatarPic.getInputStream(), avatarKey);
			if(avatarResponse.getCode() != 1){
				model.addAttribute("error", "error happen when upload avatar");
				return "regist";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
        c.setAvatar(avatarKey);
        
        Customer customer = customerManager.addCustomer(c);
		if(customer == null){
			model.addAttribute("errorInfo",  "Your username or password are userd!");
			return "regist";
		}
		else{
			return "login";
		}
	}

	@RequestMapping(value="/customer/address", method=RequestMethod.PATCH)
	@ResponseBody
	public List<Address> setDefaultAddress(@RequestBody Address address, HttpSession session){
		Customer c = (Customer) session.getAttribute("customer");
		Customer customer = customerManager.setCustomerDefaultAddress(c, address.getName());
		session.setAttribute("customer", customer);
		return customer.getAddressList();
	}

	@RequestMapping(value="customer/address", method=RequestMethod.PUT)
	@ResponseBody
	public List<Address> setDefaultAddress(String oldValue, String newValue, HttpSession session){
		Customer c = (Customer) session.getAttribute("customer");
		Customer customer = customerManager.updateCustomerAddress(c, oldValue, newValue);
		session.setAttribute("customer", customer);
		return customer.getAddressList();
	}

	@RequestMapping(value = "/customer/address", method = RequestMethod.DELETE)
	@ResponseBody
	public List<Address> deleteAddress(@RequestBody Address address, HttpSession session){
		Customer c = (Customer) session.getAttribute("customer");
		Customer customer = customerManager.deleteCustomerAddress(c, address);
		session.setAttribute("customer", customer);
		return customer.getAddressList();
	}

	@RequestMapping(value = "/customer/address", method = RequestMethod.POST)
	@ResponseBody
	public List<Address> addAddress(@RequestBody Address address, HttpSession session){
		Customer c = (Customer) session.getAttribute("customer");
		Customer customer = customerManager.addCustomerAddress(c, address);
		session.setAttribute("customer", customer);
		return customer.getAddressList();
	}

}
